package com.sskj.common.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sskj.common.enums.ApiResponseCodeEnum;
import com.sskj.common.vo.ApiResponse;
import com.sskj.common.vo.SimplePagination;

public class ApiResponseUtil {

    public static ApiResponse<Object> success() {
        ApiResponse<Object> resp = new ApiResponse<>();
        resp.setCode(ApiResponseCodeEnum.SUCCESS.getCode());
        resp.setMessage(ApiResponseCodeEnum.SUCCESS.getName());
        resp.setData(null);

        return resp;
    }

    public static <T> ApiResponse<T> success(T data) {
        ApiResponse<T> resp = new ApiResponse<>();
        resp.setCode(ApiResponseCodeEnum.SUCCESS.getCode());
        resp.setMessage(ApiResponseCodeEnum.SUCCESS.getName());
        resp.setData(data);

        return resp;
    }

    public static <T> ApiResponse<SimplePagination<T>> success(IPage<T> page) {
        ApiResponse<SimplePagination<T>> resp = new ApiResponse<>();
        resp.setCode(ApiResponseCodeEnum.SUCCESS.getCode());
        resp.setMessage(ApiResponseCodeEnum.SUCCESS.getName());

        SimplePagination<T> simplePagination = new SimplePagination<>();
        simplePagination.setTotal(page.getTotal());
        simplePagination.setRecords(page.getRecords());
        resp.setData(simplePagination);

        return resp;
    }

    public static ApiResponse<Object> fail() {
        ApiResponse<Object> resp = new ApiResponse<>();
        resp.setCode(ApiResponseCodeEnum.ERROR_EXCEPTION.getCode());
        resp.setMessage(ApiResponseCodeEnum.ERROR_EXCEPTION.getName());
        resp.setData(null);

        return resp;
    }

    public static ApiResponse<Object> fail(String message) {
        ApiResponse<Object> resp = new ApiResponse<>();
        resp.setCode(ApiResponseCodeEnum.ERROR_EXCEPTION.getCode());
        resp.setMessage(message);
        resp.setData(null);

        return resp;
    }

    public static ApiResponse<Object> fail(ApiResponseCodeEnum apiResponseCodeEnum) {
        ApiResponse<Object> resp = new ApiResponse<>();
        resp.setCode(apiResponseCodeEnum.getCode());
        resp.setMessage(apiResponseCodeEnum.getName());
        resp.setData(null);

        return resp;
    }

    public static ApiResponse<Object> fail(ApiResponseCodeEnum apiResponseCodeEnum, String message) {
        ApiResponse<Object> resp = new ApiResponse<>();
        resp.setCode(apiResponseCodeEnum.getCode());
        resp.setMessage(message);
        resp.setData(null);

        return resp;
    }
}
